package abc.database.interfaces;

import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public interface OracleHelper extends ISqlHelper<Map<String, Object>> {
    default List<Map<String, Object>> getTableList(String dbname, JdbcTemplate jdbcTemplate) {
        String sql = "select table_name, '' as table_comment, 'table' as type from user_tables where TABLESPACE_NAME is not null";
        return jdbcTemplate.queryForList(sql);
    }

    default List<Map<String, Object>> getColumnList(String dbname, JdbcTemplate jdbcTemplate) {
        List<Map<String, Object>> columns = new ArrayList<>();
        List<Map<String, Object>> tableList = this.getTableList(dbname, jdbcTemplate);
        if(tableList == null) {
            return columns;
        }
        tableList.forEach(a-> {
            if(!a.containsKey("table_name") || a.get("table_name")==null) {
                return;
            }
            String tablename = a.get("table_name").toString();
            String sql = String.format( "SELECT a.table_name table, b.column_name annotationColumnName, b.column_name propertyName, b.column_name name,b.data_type dataType,a.comments comment, 0 as isAutoIncrement, 1 as convert, 0 as versionField, 0 as keyFlag, 0 as keyIdentityFlag FROM user_col_comments a,all_tab_columns b WHERE a.table_name = b.table_name and a.table_name='%s'", tablename.toUpperCase() );
            List<Map<String, Object>> cols = jdbcTemplate.queryForList(sql);
            if(cols!=null && cols.size()>0) {
                columns.addAll(cols);
            }
        });

        return columns;
    }
}
